package cn.sw.auth.granter;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.sw.auth.entity.User;
import cn.sw.auth.entity.UserInfo;
import cn.sw.auth.entity.UserParameter;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class PasswordUserInfoGranter implements IUserInfoGranter {

    @Override
    public UserInfo grant(UserParameter userParameter) {
//        String tenantId = userParameter.getTenantId();
        String account = userParameter.getAccount();
        String password = userParameter.getPassword();

        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("admin".equals(account) && "admin".equals(password)) {
            StpUtil.login(66666);
            // 第2步，获取 Token  相关参数
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return UserInfo.builder()
                    .user((User.builder().id(10001L).build()))
                    .roles(Lists.newArrayList("1", "2", "3"))
                    .build();
        }
        return null;
    }
}
